package service

import (
	"context"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"

	"github.com/gogf/gf/v2/frame/g"
)

type sDictionary struct{}

var insDictionary = sDictionary{}

func Dictionary() *sDictionary {
	return &insDictionary
}

// AddDictionary 新增数据字典
func (s *sDictionary) AddDictionary(ctx context.Context, req *model.DictionaryInfo) (id string, err error) {
	r := g.RequestFromCtx(ctx)
	dictionaryInfo := &model.DictionaryInfoRes{
		DictionaryIdRes: &model.DictionaryIdRes{
			Id: GenInt64Id.GenInt64IdOfString(),
		},
		DictionaryInfo: req,
	}
	searchInfo := g.Map{
		"code":       req.Code,
		"class_code": req.ClassCode,
	}
	if findOne, _ := dao.LotDataDictionary.Ctx(ctx).Where(&searchInfo).One(); findOne != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "字典code不能重复")
	}
	if _, err = dao.LotDataDictionary.Ctx(ctx).Save(dictionaryInfo); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "保存字典失败")
	}
	return dictionaryInfo.Id, err
}

// UpdateDictionary 编辑数据字典
func (s *sDictionary) UpdateDictionary(ctx context.Context, req *api.UpdateDictionaryReq) (id string, err error) {
	_, err = dao.LotDataDictionary.Ctx(ctx).Where("id", req.Id).Update(req)
	return req.Id, err
}

// DeleteDictionary  删除数组字典
func (s *sDictionary) DeleteDictionary(ctx context.Context, req *api.DeleteDictionaryReq) (id string, err error) {
	_, err = s.SearchDictionary(ctx, &model.SearchDictionary{
		Id: req.Id, // 字典的id
	})
	_, err = dao.LotDataDictionary.Ctx(ctx).Where("id", req.Id).Delete(req)
	return id, err
}

// SearchDictionaryList  获取数据字典数组
func (s *sDictionary) SearchDictionaryList(ctx context.Context, req *model.SearchDictionaryListReq) (res *model.DictionaryListRes, err error) {
	searchInfo := &model.SearchDictionary{
		ClassName: req.ClassName,
		Name:      req.Name,
		ClassCode: req.ClassCode,
	}
	findList := make([]*model.DictionaryInfoRes, 0)
	// 获取分页偏移量
	startNum, endNum := paging.Paging(req.Paging)
	_ = dao.LotDataDictionary.Ctx(ctx).OmitEmpty().Where(searchInfo).OrderDesc("created_at").Limit(startNum, endNum).Scan(&findList)
	// 获取总条数
	countNum, err := dao.LotDataDictionary.Ctx(ctx).OmitEmpty().Where(searchInfo).Count()
	// 获取总页码
	PutPageList := paging.PutCount(req.Paging, countNum)
	res = &model.DictionaryListRes{
		List:    findList,
		PutPage: *PutPageList,
	}
	return res, err
}

// SearchDictionaryOne 通过id获取数据字典
func (s *sDictionary) SearchDictionaryOne(ctx context.Context, req *api.SearchDictionaryOneReq) (res *model.DictionaryInfoRes, err error) {
	searchInfo := &model.SearchDictionary{Id: req.Id}
	_ = dao.LotDataDictionary.Ctx(ctx).OmitEmpty().Where(searchInfo).Scan(&res)
	return res, err
}

// SearchDictionary 通过字典code查询
func (s *sDictionary) SearchDictionary(ctx context.Context, req *model.SearchDictionary) (res *model.DictionaryInfoRes, err error) {
	r := g.RequestFromCtx(ctx)
	_ = dao.LotDataDictionary.Ctx(ctx).OmitEmpty().Where(req).Scan(&res)
	if res == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有该字典")
	}
	return res, err
}
